import { Router } from "vue-router";
import { storage } from "@/utils/storage";
import { ACCESS_TOKEN } from "@/store/mutation-type";
import { useUser } from "@/store/modules/user";
import { PageEnum } from "@/enums/pageEnum";
import designSetting from "@/settings/designSetting";

// const { darkTheme, appTheme, appThemeList } = designSetting;

const baseHome = PageEnum.BASE_HOME as string;
const loginPath = PageEnum.BASE_LOGIN as string;
const registerPath = PageEnum.BASE_REGISTER as string;
const whiteList = [loginPath, registerPath];

export function setupRouterGuard(router: Router) {
  const userStore = useUser();
  const Loading = window["$loading"] || null;
  Loading && Loading.start();
  router.beforeEach(async (to, from, next) => {
    console.log(`to`, to, `from`, from);

    // if (whiteList.includes(to.path)) {
    //   next();
    //   return;
    // }
    // //检查用户是否登录并且token是否存在
    // const token = storage.get(ACCESS_TOKEN);
    // if (to.meta.requiresAuth && !token) {
    //   next({
    //     path: loginPath,
    //   });
    //   return;
    // }

    // // 如果用户已登录但尝试访问登录页
    // if (to.path === loginPath && token) {
    //   next({ path: baseHome });
    //   return;
    // }
    // console.log(`123456`, 123456);
    const userInfo = await userStore.getInfo();

    // 其他情况正常放行
    next();
  });
  router.afterEach((to, from) => {
    Loading && Loading.finish();
  });
}
